package android.moodle;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;

/**
 * Created by IntelliJ IDEA.
 * User: michelle
 * Date: 5/1/11
 * Time: 10:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class NewGameOnPhoneActivity extends Activity {
    private int numPlayers;
    private Context context;
    private int playMode;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_game_phone);

        context = this;

        RadioButton radioTwoPl = (RadioButton)findViewById(R.id.radioTwoPl);
        RadioButton radioThreePl = (RadioButton)findViewById(R.id.radioThreePl);

        radioTwoPl.setOnClickListener(radioListener);
        radioThreePl.setOnClickListener(radioListener);


    }


    View.OnClickListener radioListener = new View.OnClickListener() {
        public void onClick(View v) {
            RadioButton button = (RadioButton) v;
            LinearLayout linLayout = (LinearLayout)findViewById(R.id.playerNamesLayout);
            linLayout.setVisibility(View.VISIBLE);
            EditText player1 = (EditText)findViewById(R.id.player1Name);
            player1.setVisibility(View.VISIBLE);

            int id = v.getId();
            switch (id) {
                case R.id.radioTwoPl:
                    numPlayers = 2;
                    showEditFields();
                    showRadioButtons();
                    System.out.println("Verdi " + numPlayers);
                    break;
                case R.id.radioThreePl:
                    numPlayers = 3;
                    showEditFields();
                    showRadioButtons();
                    break;
            }

        }
    };

    View.OnClickListener typeOfGameRadioListener = new View.OnClickListener() {
        public void onClick(View v) {
            RadioButton button = (RadioButton) v;
            Button startGame =(Button) findViewById(R.id.createGameBtn);
            startGame.setVisibility(Button.VISIBLE);
            int id = v.getId();
            switch (id) {
                case R.id.radioThree:
                    playMode = 3;
                    break;
                case R.id.radioFour:
                    playMode = 4;
                    break;
                case R.id.radioFive:
                    playMode = 5;
                    break;
            }

        }
    };


    private void showRadioButtons() {
        RadioButton radioThree = (RadioButton)findViewById(R.id.radioThree);
        RadioButton radioFour = (RadioButton)findViewById(R.id.radioFour);
        RadioButton radioFive = (RadioButton)findViewById(R.id.radioFive);

        radioThree.setOnClickListener(typeOfGameRadioListener);
        radioFour.setOnClickListener(typeOfGameRadioListener);
        radioFive.setOnClickListener(typeOfGameRadioListener);

        if(numPlayers == 2) {
            radioThree.setVisibility(View.VISIBLE);
            radioFour.setVisibility(View.VISIBLE);
            radioFive.setVisibility(View.VISIBLE);
        } else if(numPlayers > 2) {
            if(radioFour.getVisibility() == View.VISIBLE) radioFour.setVisibility(View.GONE);
            if (radioFive.getVisibility() == View.VISIBLE) radioFive.setVisibility(View.GONE);
            radioThree.setVisibility(View.VISIBLE);
        }
    }

    private void showEditFields() {
        EditText player3 = (EditText)findViewById(R.id.player3Name);
        EditText player2 = (EditText)findViewById(R.id.player2Name);

        if(player2.getVisibility() != View.VISIBLE) player2.setVisibility(View.VISIBLE);
        if(numPlayers == 2 && player3.getVisibility() == View.VISIBLE) player3.setVisibility(View.GONE);
        if(numPlayers == 3 && player3.getVisibility() != View.VISIBLE) player3.setVisibility(View.VISIBLE);
    }

    public void startGame(View v) {
        Intent intent = new Intent(this, GameOnPhoneActivity.class);
        intent.putExtra("numPlayers", numPlayers);
        intent.putExtra("typeOfGame", playMode);
        startActivity(intent);
    }

    private void backBtn(View v) {
        finish();
    }
}